package com.tjnu.losca.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjnu.losca.core.ResponseVO;
import com.tjnu.losca.pojo.Grade;
import com.tjnu.losca.pojo.Studentbygrade;
import com.tjnu.losca.service.IGradeService;
import com.tjnu.losca.service.IStudentbygradeService;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * <p>
 * 班级 前端控制器
 * </p>
 *
 * @author losca
 * @since 2021-10-05
 */
@RestController
@RequestMapping("/grade")
public class GradeController {

    @Autowired
    private IGradeService gradeService;
    @Autowired
    private IStudentbygradeService studentbygradeService;

    @PostMapping("/add")
    public ResponseVO<Boolean> add(Grade grade){
        return ResponseVO.success("添加班级成功",gradeService.save(grade));
    }

    @GetMapping("/check")
    public ResponseVO<Boolean> check(Integer id,String value){
        boolean check = gradeService.check(id, value);
        if (check){
            return ResponseVO.success("验证通过",gradeService.check(id,value));
        }
        return ResponseVO.failed("存在相同数据");
    }

    @GetMapping("/getAll")
    public ResponseVO<IPage> getAll(@RequestParam(defaultValue = "1") int pageNum,
                                    @RequestParam(defaultValue = "3") int pageSize) {
        Page<Grade> page = new Page<>(pageNum, pageSize);
        return ResponseVO.success("成功", gradeService.page(page));
    }

    @GetMapping("/getDataByName")
    public ResponseVO<IPage> getDataBySearch(@RequestParam(defaultValue = "1") int pageNum,
                                             @RequestParam(defaultValue = "3") int pageSize,
                                             @RequestParam(defaultValue = "") String name) {
        return ResponseVO.success("查询成功", gradeService.getDataByName(pageNum, pageSize, name.trim()));
    }

    @GetMapping("/getGradeById")
    public ResponseVO<Grade> getGradeById(Integer id){
        return ResponseVO.success("成功", gradeService.getById(id));
    }

    @PostMapping("/update")
    public ResponseVO<Boolean> update(Grade grade){
        return ResponseVO.success("修改班级成功", gradeService.updateById(grade));
    }

    @GetMapping("/del")
    public ResponseVO<Boolean> del(Integer id) {
        return ResponseVO.success("删除成功", gradeService.del(id));
    }

    @GetMapping("/recovery")
    public ResponseVO<Boolean> recovery(Integer id){
        return ResponseVO.success("恢复成功", gradeService.recovery(id));
    }

    //execl文件处理
    @PostMapping("/upload")
    public ResponseVO<Boolean> upload(MultipartFile file, Integer id) throws IOException, ParseException {
        Workbook workbook = null;
        String suffix = FilenameUtils.getExtension(file.getOriginalFilename());
        if ("xlsx".equalsIgnoreCase(suffix)){
            workbook = new XSSFWorkbook(file.getInputStream());
        }else {
            workbook = new HSSFWorkbook(file.getInputStream());
        }
        Sheet sheetAt = workbook.getSheetAt(0);
        //获取最后一行行数
        int lastRowNum = sheetAt.getLastRowNum();
        ArrayList<Studentbygrade> list = new ArrayList<>();
        for (int i = 1; i <= lastRowNum; i++) {
            Row row = sheetAt.getRow(i);
            Studentbygrade xml = new Studentbygrade();
            //把数据存在列表里面
            xml.setName(row.getCell(0).getStringCellValue().trim());
            xml.setEmail(row.getCell(1).getStringCellValue().trim());
            xml.setSex(row.getCell(2).getStringCellValue().trim());
            String trim = row.getCell(3).getStringCellValue().trim();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
            Date birth = simpleDateFormat.parse(row.getCell(3).getStringCellValue().trim());
            xml.setBirth(birth);
            xml.setGradeId(id);
            list.add(xml);
        }
        //把学生插入数据库
        studentbygradeService.saveBatch(list);
        //更新班级人数
        QueryWrapper<Studentbygrade> wrapper = new QueryWrapper<>();
        wrapper.eq("gradeId",id);
        int count = studentbygradeService.count(wrapper);
        Grade grade = new Grade();
        grade.setId(id);
        grade.setCount(count);
        gradeService.updateById(grade);
        return ResponseVO.success("导入成功",true);
    }

    /**
     * 通过班级id获得学生信息
     * @param id 班级id
     * @return
     */
    @GetMapping("/getDetails")
    public ResponseVO<IPage> getDetails(@RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "3") Integer pageSize,
                                        Integer id){
        return ResponseVO.success("查询成功",gradeService.getDetails(pageNum,pageSize,id));
    }

}
